#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(register int i = (a);i <= (b);++i)
#define per(i,a,b) for(register int i = (a);i >= (b);--i)  
#define cases int _;cin >> _;for(register int i = 1;i <= _;++i)
typedef long long ll;
typedef unsigned long long ull;
using std::string;using std::cin;using std::cout;

const int N = 1e5+5;
const ll P = 1e9+7;
int fa[N],s1[N],s2[N],n;
ll tot1,tot2,ans;

inline int getfa(int x){
    if(fa[x] != x) return fa[x] = getfa(fa[x]);
    else return fa[x] = x;
}

int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("in.in", "r", stdin);
    cases{
        cin >> n;
        int k,u,v;
        tot1 = tot2 = 0;
        rep(i,1,n){
            cin >> k;
            if(k == 1) s1[i] = 1 , s2[i] = 0 , ++tot1;
            else s1[i] = 0 , s2[i] = 1 , ++tot2;
            fa[i] = i;
        }
        ans = (tot2 * (tot2-1) * (tot2-2) / 6)%P + (tot2 * (tot2-1) * tot1 / 2)%P + P , ans %= P;
        cout << ans << "\n";
        rep(i,1,n-1){
            cin >> u >> v;
            u = getfa(fa[u]) , v = getfa(fa[v]);
            // cout << "u = " << u << "   v = " << v << " \n";
            // cout << s1[u] << " " << s2[u] << "    " << s1[v] << " " << s2[v] << "\n";
            ans = (ans - s2[u] * s2[v] * (tot2 - s2[u] - s2[v])%P + P)%P,
            ans = (ans - s2[u] * s1[v] * (tot2 - s2[u] - s2[v])%P + P)%P,
            ans = (ans - s1[u] * s2[v] * (tot2 - s2[u] - s2[v])%P + P)%P,
            ans = (ans - s2[u] * s2[v] * (tot1 - s1[u] - s1[v])%P + P)%P;
            fa[u] = v;
            s1[v] += s1[u] , s2[v] += s2[u];
            cout << ans << "\n";
        }
    }

    return 0;
}